/***************************************************************************
Name:		chathandler.cpp
Autor:		Kevin Huber
Version:	0.2
Last Edit:      17.07.2009

Beschreibung:
Diese Klasse behandelt alle eingehenden Pakete
***************************************************************************/

#include "chathandler.h"

ChatHandler::ChatHandler(QObject *parent) : QObject(parent)
{
}

ChatHandler::~ChatHandler()
{
}

void ChatHandler::handlePackage(Package *package)
{
    if( package->typ == TUNNEL_PACKAGETYP_MESSAGE )
    {
        handleNewMessage(package->message, package->to);

        return;
    }
    else if( package->typ == TUNNEL_PACKAGETYP_SYSTEM )
    {
        handleSystemCommand(package->commandCategory, package->command, package->arguments);

        return;
    }

    printf("Unhandled Package from typ: %d\n", package->typ);
    return;
}

void ChatHandler::handleNewMessage(const QString &message, const QString &to)
{
    if( message.length() < 1 )
        return;

    if( to.length() < 1 )
        emit newGlobalMessage(message);
    else
        emit newPrivateMessage(message, to);
}

void ChatHandler::handleSystemCommand(const quint16 &category, const quint16 &command, const QString &arguments)
{
    if( category == TUNNEL_COMMANDCATEGORY_COMM )
    {
        switch(command)
        {
        case TUNNEL_COMMAND_COMM_AUTH:
            emit authCommand(arguments);
            break;
        default:
            emit error("Unknown Comm Command: "+command);
        }
    }
    else if( category == TUNNEL_COMMANDCATEGORY_FRIENDS )
    {
        switch(command)
        {
        case TUNNEL_COMMAND_FRIENDS_UPDATEFRIENDLIST:
            emit getFriendlist();
            break;
        case TUNNEL_COMMAND_FRIENDS_ADDFRIEND:
            emit addFriend(arguments);
            break;
        case TUNNEL_COMMAND_FRIENDS_GETFRIENDREQUESTS:
            emit getFriendrequests();
            break;
        case TUNNEL_COMMAND_FRIENDS_ACCEPTFRIENDINVENTATION:
            emit acceptFriendInventation(arguments.toULongLong());
            break;
        default:
            emit error("Unknown Friends Command: "+command);
        }
    }
    else if( category == TUNNEL_COMMANDCATEGORY_CLIENT )
    {
        switch(command)
        {
        case TUNNEL_COMMAND_CLIENT_CHANGENICKNAME:
            emit changeNickname(arguments);
            printf("ChatHandler::handleSystemCommand() [%s]\n", arguments.toStdString().c_str());
            break;
        default:
            emit error("Unknown Client Command: "+command);
        }
    }
    else
        emit error("Unknown System Command Categorie: "+category);
}
